package com.ss.android.article.base.feature.user.location;

import android.content.Context;
import android.content.Intent;
import android.widget.ListView;

import com.ss.android.article.base.feature.user.location.model.Location;
import com.ss.android.article.base.feature.user.location.model.LocationResult;
import com.ss.android.article.browser.R;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.newmedia.activity.FragmentActivity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ProvinceSelectedActivity extends FragmentActivity {
    private List a;
    private ListView b;
    private com.ss.android.article.base.feature.user.location.c c;
    private LocationResult d;
    private c.a e;

    public ProvinceSelectedActivity() {
        this.a = new ArrayList();
        this.e = new c.a(this);
    }

    static LocationResult a(ProvinceSelectedActivity arg1) {
        return arg1.d;
    }

    static LocationResult a(ProvinceSelectedActivity arg0, LocationResult arg1) {
        arg0.d = arg1;
        return arg1;
    }

    private void a() {
        Intent v0 = new Intent();
        v0.putExtra("location", this.d);
        this.setResult(-1, v0);
        this.finish();
    }

    private String b() {
        BufferedReader v2_3;
        String v0_3;
        BufferedReader v1 = null;
        InputStream v3 = null;
        InputStream v2 = null;
        try {
            v3 = this.getAssets().open("location.json");
            v1 = new BufferedReader(new InputStreamReader(v3));
            StringBuilder v0_2 = new StringBuilder();
            while (true) {
                String v2_1 = v1.readLine();
                if (v2_1 == null) {
                    break;
                }
                v0_2.append(v2_1);
            }
            v0_3 = v0_2.toString();
            return v0_3;
        } catch (IOException v0_1) {
            return "";
        } finally {
            if(v3 != null){
                try{
                    v3.close();
                }catch (Exception e){}
            }

            if(v1 != null){
                try{
                    v1.close();
                }catch (Exception e){}
            }
        }
    }

    static void b(ProvinceSelectedActivity arg0) {
        arg0.a();
    }

    public int getLayout() {
        return R.layout.a5;
    }

    public void init() {
        super.init();
        this.mTitleView.setText(com.ss.android.article.browser.R.string.a0a);
        this.b = (ListView) this.findViewById(R.id.hv);
        this.c = new c();
        this.c.a = this.e;
        this.b.setAdapter(this.c);
        String v0 = this.b();
        try {
            JSONArray v3 = new JSONArray(v0);
            int v2;
            for (v2 = 0; v2 < v3.length(); ++v2) {
                JSONObject v0_2 = v3.getJSONObject(v2);
                String v4 = v0_2.optString("province", "");
                ArrayList v5 = new ArrayList();
                JSONArray v6 = v0_2.optJSONArray("areas");
                int v0_3;
                for (v0_3 = 0; v0_3 < v6.length(); ++v0_3) {
                    ((List) v5).add(v6.optString(v0_3));
                }

                Location v0_4 = new Location();
                v0_4.province = v4;
                v0_4.areas = ((List) v5);
                this.a.add(v0_4);
            }

            this.c.a(this.a);
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        }

        this.c.a(this.a);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == 110 && resultCode == -1) {
            String v0 = intent.getStringExtra("area");
            if (this.d != null) {
                this.d.area = v0;
            }

            this.a();
        }
    }

    public void onBackPressed() {
        if (!this.isFinishing()) {
            Intent v0 = null;
            if (this.isTaskRoot()) {
                v0 = ToolUtils.getLaunchIntentForPackage(((Context) this), this.getPackageName());
            }

            this.finish();
            if (v0 == null) {
                return;
            }

            this.startActivity(v0);
        }
    }
}

